from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from auth import get_current_active_user, UserPublic
from db import get_db
from models import Root


router = APIRouter(prefix="/api/root", tags=["超级用户"])
@router.get("/all")
def get_user_all(db: Session = Depends(get_db)):
    return db.query(Root).all()

@router.get("/{user_id}")
def get_user(user_id: int, db: Session = Depends(get_db)):
  user = db.query(Root).filter(Root.id == user_id).first()
  if user is None:
    raise HTTPException(status_code=404, detail="User not found")
  user.avatar = f"http://127.0.0.1:8000/{user.avatar}" if user.avatar else ""
  return user


@router.get("/current_user")
def get_current_active_user(current_user: UserPublic = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return current_user
